package com.sky.admin.controller;


//import com.sky.annotation.AutoFill;
import com.sky.constant.PasswordConstant;
import com.sky.context.BaseContext;
import com.sky.enumeration.OperationType;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.admin.entity.dto.EmployeeLoginDTO;
import com.sky.admin.entity.dto.EmployeePageQueryDTO;
import com.sky.admin.entity.dto.PasswordEditDTO;
import com.sky.admin.entity.po.Employee;
import com.sky.admin.entity.vo.EmployeeLoginVO;
import com.sky.admin.service.IEmployeeService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 员工信息 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-09-03
 */
@RestController
@RequestMapping("/admin/employee")
@Slf4j
@RequiredArgsConstructor
public class EmployeeController {

    private final IEmployeeService employeeService;
    /*
    * 员工登陆
    * */
    @PostMapping("/login")
    @ApiOperation("员工登陆")
    public Result<EmployeeLoginVO> employeeLogin(@RequestBody EmployeeLoginDTO employeeLoginDTO){
        log.info("员工登陆：{}",employeeLoginDTO);
        EmployeeLoginVO employeeLoginVO = employeeService.login(employeeLoginDTO);
        return  Result.success(employeeLoginVO);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启动/禁用员工账号")
    public Result updateStatus(@PathVariable Integer status,Long id){
        log.info("启动禁用员工账号：{}，{}",status,id);
        employeeService.updateStatus(status,id);
        return Result.success();
    }

    @PutMapping("/editPassword")
    @ApiOperation("修改密码")
    public Result updatePassword(@RequestBody PasswordEditDTO passwordEditDTO){
        log.info("员工修改密码: {}",passwordEditDTO);
        employeeService.updatePassword(passwordEditDTO);
        return Result.success();
    }

    /**
     * 退出功能
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出登录")
    public Result<String> logout() {
        BaseContext.removeCurrentId();
        return Result.success("已退出账号");
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id获取员工信息")
    public Result<Employee> getById(@PathVariable Long id){
        log.info("获取id为：{}的员工信息",id);
        return Result.success(employeeService.getById(id));
    }

    @PutMapping
    @ApiOperation("修改员工信息")
//    @AutoFill(OperationType.UPDATE)
    public Result updateEmployee(@RequestBody Employee employee){
        log.info("修改员工信息：{}",employee);
        employeeService.updateById(employee);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("员工分页查询")
    public Result<PageResult> employeePageQuery(Integer page, Integer pageSize,String name){
        log.info("员工分页查询：{},{}",page,pageSize);
        EmployeePageQueryDTO employeePageQueryDTO = EmployeePageQueryDTO.builder()
                .pageSize(pageSize)
                .page(page)
                .name(name)
                .build();
        return Result.success(employeeService.pageQuery(employeePageQueryDTO));
    }

    @PostMapping
    @ApiOperation("新增员工信息")
//    @AutoFill(OperationType.INSERT)
    public Result insertEmployee(@RequestBody Employee employee){
        log.info("新增员工：",employee);

        employee.setPassword(DigestUtils.md5DigestAsHex(PasswordConstant.DEFAULT_PASSWORD.getBytes()));

        employeeService.save(employee);
        return Result.success();
    }

}
